//
//  MBOrderViewModel.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/24.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBOrderViewModel.h"
#import "MBOrderTableCellViewModel.h"
#import "MBOrderSectionHeaderView.h"
#import "MBOrderSectionFooterView.h"
#import "MBOrderModel.h"
#import "MBOrderNetTool.h"

@implementation MBOrderViewModel

-(void)setUpData
{
    [super setUpData];
}

-(MBOrderListParam *)param
{
    if (_param == nil) {
        _param = [[MBOrderListParam alloc]init];
        _param.order_status = -1;
    }
    return _param;
}

-(void)sendRequest:(MBRequestResult)result
{
    MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
    self.param.store_id = userModel.store_id;
    self.param.p = self.currentPage;
    self.param.token = userModel.token;
    [MBOrderNetTool netToolPostOrderListParam:self.param success:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            NSArray* array = response.result;
            NSMutableArray* list = [NSMutableArray array];
            for (int i=0; i<array.count; i++) {
                MBOrderModel* model = [MBOrderModel yy_modelWithDictionary:array[i]];
                model.isGroup = NO;
                [list addObject:model];
            }
            [self factoryInitDataModelArray:list totalCount:10 cellViewModelClass:[MBOrderTableCellViewModel class]];
            result(YES);
        }else {
            result(NO);
            [InterfaceHUD showErrorHUDWithString:response.msg];
        }
    } failure:^(NSError *error) {
        result(NO);
        [InterfaceHUD showErrorHUDWithString:@"获取订单失败，请稍后重试！"];
    }];
}

-(void)sendGroupRequest:(MBRequestResult)result
{
    MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
    self.param.store_id = userModel.store_id;
    self.param.p = self.currentPage;
    self.param.token = userModel.token;
    [MBOrderNetTool netToolPostOrderGroupListParam:self.param success:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            NSArray* array = response.result;
            NSMutableArray* list = [NSMutableArray array];
            for (int i=0; i<array.count; i++) {
                MBOrderModel* model = [MBOrderModel yy_modelWithDictionary:array[i]];
                model.isGroup = YES;
                [list addObject:model];
            }
            [self factoryInitDataModelArray:list totalCount:10 cellViewModelClass:[MBOrderTableCellViewModel class]];
            result(YES);
        }else {
            result(NO);
            [InterfaceHUD showErrorHUDWithString:response.msg];
        }
    } failure:^(NSError *error) {
        result(NO);
        [InterfaceHUD showErrorHUDWithString:@"获取订单失败，请稍后重试！"];
    }];
}

-(NSInteger)numberOfSections
{
    return self.dataArray.count;
}

-(NSInteger)numberOfRowInSection:(NSInteger)section
{
    MBOrderTableCellViewModel* model = self.dataArray[section];
    return model.dataModel.goods.count;
}

-(CGFloat)heightForHeaderInSection:(NSInteger)section
{
    return [UIView lf_sizeFromIphone6:49];
}

-(CGFloat)heightForFooterInSection:(NSInteger)section
{
    return [UIView lf_sizeFromIphone6:80];
}

-(UIView *)viewForHeaderInSection:(NSInteger)section
{
    MBOrderSectionHeaderView* headerView = [[MBOrderSectionHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, [UIView lf_sizeFromIphone6:49])];
    MBOrderTableCellViewModel* model = self.dataArray[section];
    headerView.viewModel = model;
    return headerView;
}

-(UIView *)viewForFooterInSection:(NSInteger)section
{
    MBOrderSectionFooterView* footerView = [[MBOrderSectionFooterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, [UIView lf_sizeFromIphone6:80])];
    MBOrderTableCellViewModel* model = self.dataArray[section];
    footerView.viewModel = model;
    @weakify(self);
    [[[footerView.shureButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:footerView.rac_willDeallocSignal]subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (footerView.viewModel.dataModel.order_status == 0) {
            //            确认
            MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
            MBOrderConfirmParam* confirmModel = [[MBOrderConfirmParam alloc]init];
            confirmModel.store_id = userModel.store_id;
            confirmModel.order_id = footerView.viewModel.dataModel.order_id;
            confirmModel.token = userModel.token;
            [MBOrderNetTool netToolPostOrderConfirmParam:confirmModel success:^(MBHTTPResponse *response) {
                if (response.status == 1) {
                    footerView.viewModel.dataModel.order_status = 1;
                    self.isNeedReload = YES;
                }else {
                    [InterfaceHUD showErrorHUDWithString:response.msg];
                }
            } failure:^(NSError *error) {
                [InterfaceHUD showErrorHUDWithString:@"确认失败，请稍后重试！"];
            }];
        }else {
            //            发货
            self.shippingOrderModel = footerView.viewModel.dataModel;
        }
    }];
    return footerView;
}

-(void)sendGoodsDeliverHandleWithParam:(MBOrderConfirmParam*)param andOrderModel:(MBOrderModel*)orderModel
{
    [MBOrderNetTool netToolPostOrderDeliveryHandleParam:param success:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            [InterfaceHUD showSuccessHUDWithString:response.msg];
            orderModel.shipping_status = 1;
            self.isNeedRefresh = YES;
        }else {
            [InterfaceHUD showErrorHUDWithString:response.msg];
        }
    } failure:^(NSError *error) {
        [InterfaceHUD showErrorHUDWithString:@"发货失败，请稍后重试！"];
    }];
}

-(MBBaseTableCellViewModel *)cellViewModelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.dataArray[indexPath.section];
}


@end
